﻿using UnityEngine;
using System.Collections;

public class camerashake : MonoBehaviour {
	
	public bool shaking;
	private Vector3 OriginalPos;

	void Awake()
	{

	}
	// Use this for initialization
	void Start () {
		//shaking = true;
		UpdateOriginalPos();
		//print ("sending...");
		//print ("sending event... to handle");
	}

	public void UpdateOriginalPos()
	{
		OriginalPos = transform.position;
	}

	// Update is called once per frame
	void Update () {
		if(shaking == true)
			onCameraShake();
	}

	public void onCameraShake()
	{
		StartCoroutine (cameraShake ());
		shaking = true;
	}
	
	IEnumerator cameraShake()
	{
		if(shaking == true)
		{
			transform.position = new Vector3 (Mathf.PingPong (Time.time * 12, 1), transform.position.y, -10);
		}
		yield return new WaitForSeconds (0.45f);
		shaking = false;
		if(transform.position != OriginalPos)
			transform.position = OriginalPos;
	}
}
